// pages/selectGoods/selectGoods.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goods_attrSelect: [],
    good: [],
    goodslide: [],
    goodintro: [],
    goodparameter: [],
    goodversion: [],
    curcIndex: 0,
    curvIndex: 0,
    select_num: 1,
    cover_img: "",
    memory: "",
    price: "50",
    Authorization: ''
  },
  selectVersion(e) {
    const version = e.detail.value;
    // console.log(version);
    const versionId = version.split(",")[0];
    const memory = version.split(",")[1];
    const price = version.split(",")[2];
    // console.log(price);
    // wx.setStorageSync('version', version);
    wx.setStorageSync('versionId', versionId);
    wx.setStorageSync('memory', memory);
    wx.setStorageSync('price', price);
    this.setData({
      memory,
      price
    });
  },
  versionHasSelected(e) {
    const curvIndex = e.currentTarget.dataset.index ? e.currentTarget.dataset.index : 0;
    console.log(curvIndex);
    this.setData({
      curvIndex
    });
  },
  minusCount(e) {
    let select_num = this.data.select_num;
    select_num--;
    if (select_num < 1) {
      return;
    }
    this.setData({
      select_num
    });
    wx.setStorageSync('select_num', select_num);
  },
  addCount(e) {
    let select_num = this.data.select_num;
    select_num++;
    if (select_num > 5) {
      return;
    }
    this.setData({
      select_num
    });
    wx.setStorageSync('select_num', select_num);
  },
  submit(e) {
    const pre_item = wx.getStorageSync('attr_item');
    console.log(pre_item);
    const temp = {
      'versionId': wx.getStorageSync('versionId'),
      'goodId': wx.getStorageSync('goodId'),
      'goods_name': wx.getStorageSync('goods_name'),
      'memory': wx.getStorageSync('memory'),
      'price': wx.getStorageSync('price'),
      // 'version': wx.getStorageSync('version'),
      'select_num': wx.getStorageSync('select_num'),
      'cover': wx.getStorageSync('cover'),
      'selected': false,
      'isTouchMove': false
    }
    wx.setStorageSync('attr_item', [temp, ...pre_item]);
    wx.showToast({
      title: '已加入购物车',
      icon: 'success',
      duration: 3000,
      success() {
        setTimeout(() => {
          wx.navigateBack({
            url: "../goods/show"
          });
        }, 1000)
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    const id = options.id;
    var Authorization=wx.getStorageSync('Authorization');
    if(Authorization==''){
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
    }
    that.setData({
      Authorization: Authorization,
    })
    console.log(id);
    wx.request({
      url: "http://localhost:8080/wechat/selectgooddetail",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'id': id,
      },
      success: function (res) {
        if (res.statusCode == 200) {
          that.setData({
            good: res.data["goods"],
            goodslide: res.data["goodSlideList"],
            goodintro: res.data["goodIntroList"],
            goodparameter: res.data["goodParameterList"],
            goodversion: res.data['goodVersionList'],
          })
          var prices = that.data.goodversion[0].price;
          var memory = that.data.goodversion[0].value;
          that.setData({
            price: prices,
            memory: memory,
          })
          const versionId = that.data.goodversion[0].versionId;
          const cover_img = that.data.goodslide[0].image;
          wx.setStorageSync('goodId', that.data.good.id);
          wx.setStorageSync('versionId', versionId);
          wx.setStorageSync('memory', that.data.memory);
          wx.setStorageSync('price', that.data.price);
          wx.setStorageSync('cover', cover_img);
          wx.setStorageSync("goods_name", that.data.good.header);
          console.info(res);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
      }
    })
    this.setData({
      memory: '',
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const version = wx.getStorageSync('version')
    console.log(version)
    wx.setStorageSync('select_num', this.data.select_num);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})